package com.vegetable.modules.service.baseData.impl;

import com.vegetable.modules.entity.baseData.ProductCategory;
import com.vegetable.modules.service.baseData.IProductCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 商品分类数据初始化
 * </p>
 *
 * @author wangruijie
 * @since 2024-12-10
 */
@Slf4j
@Component
public class ProductCategoryDataInitializer implements ApplicationRunner {

    @Autowired
    private IProductCategoryService productCategoryService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 检查是否已经初始化过分类数据
        long count = productCategoryService.count();
        if (count > 0) {
            log.info("商品分类数据已存在，跳过初始化");
            return;
        }

        log.info("开始初始化商品分类数据...");
        initCategoryData();
        log.info("商品分类数据初始化完成");
    }

    /**
     * 初始化分类数据
     */
    private void initCategoryData() {
        // 一级分类
        createCategory(1L, "蔬菜品类", "VEGETABLE", 0L, 1, 1, "各种蔬菜类商品");
        createCategory(2L, "特殊品类", "SPECIAL", 0L, 1, 2, "特殊类型蔬菜商品");
        createCategory(3L, "包装类型", "PACKAGE", 0L, 1, 3, "按包装方式分类");
        createCategory(4L, "产地来源", "ORIGIN", 0L, 1, 4, "按产地来源分类");
        createCategory(5L, "处理方式", "PROCESS", 0L, 1, 5, "按处理方式分类");

        // 蔬菜品类下的二级分类
        createCategory(11L, "叶菜", "VEGETABLE_LEAF", 1L, 2, 1, "叶类蔬菜");
        createCategory(12L, "根茎", "VEGETABLE_ROOT", 1L, 2, 2, "根茎类蔬菜");
        createCategory(13L, "茄果", "VEGETABLE_FRUIT", 1L, 2, 3, "茄果类蔬菜");
        createCategory(14L, "辣椒", "VEGETABLE_PEPPER", 1L, 2, 4, "辣椒类蔬菜");
        createCategory(15L, "瓜类", "VEGETABLE_MELON", 1L, 2, 5, "瓜类蔬菜");
        createCategory(16L, "南瓜类", "VEGETABLE_PUMPKIN", 1L, 2, 6, "南瓜类蔬菜");

        // 特殊品类下的二级分类
        createCategory(21L, "水果类", "SPECIAL_FRUIT", 2L, 2, 1, "水果型蔬菜");
        createCategory(22L, "特菜类", "SPECIAL_VEGETABLE", 2L, 2, 2, "特色蔬菜");

        // 包装类型下的二级分类
        createCategory(31L, "包装类", "PACKAGE_STANDARD", 3L, 2, 1, "标准包装");
        createCategory(32L, "小包装", "PACKAGE_SMALL", 3L, 2, 2, "小包装商品");
        createCategory(33L, "冷库货", "PACKAGE_COLD", 3L, 2, 3, "冷库存储商品");

        // 产地来源下的二级分类
        createCategory(41L, "聊城", "ORIGIN_LIAOCHENG", 4L, 2, 1, "聊城产地");
        createCategory(42L, "张掖", "ORIGIN_ZHANGYE", 4L, 2, 2, "张掖产地");
        createCategory(43L, "沽源", "ORIGIN_GUYUAN", 4L, 2, 3, "沽源产地");

        // 处理方式下的二级分类
        createCategory(51L, "卸车类", "PROCESS_UNLOAD", 5L, 2, 1, "卸车处理类商品");
    }

    /**
     * 创建分类
     */
    private void createCategory(Long id, String categoryName, String categoryCode, 
                               Long parentId, Integer level, Integer sortOrder, String description) {
        ProductCategory category = new ProductCategory();
        category.setId(id);
        category.setCategoryName(categoryName);
        category.setCategoryCode(categoryCode);
        category.setParentId(parentId);
        category.setLevel(level);
        category.setSortOrder(sortOrder);
        category.setDescription(description);
        category.setIsEnabled(1);
        category.setCreateUserId(1L);

        try {
            productCategoryService.save(category);
            log.debug("创建分类成功: {}", categoryName);
        } catch (Exception e) {
            log.error("创建分类失败: {}, 错误: {}", categoryName, e.getMessage());
        }
    }
}